﻿using System;
using System.IO;
using System.Text;

namespace FileTransfer.Assistantor
{
    /// <summary>
    /// 文件助手
    /// </summary>
    public class FileAssistantor
    {
        /// <summary>
        /// 获取字节长度字符
        /// </summary>
        /// <param name="resLength">字节数</param>
        /// <returns></returns>
        public static string GetLengthStr(long resLength)
        {
            if (resLength >= (long)1024 * 1024 * 1024)
            {
                return string.Format("{0}GB ({1}bytes)", Math.Round((double)resLength / (1024 * 1024 * 1024), 2), resLength);
            }
            else if (resLength >= (long)1024 * 1024)
            {
                return string.Format("{0}MB ({1}bytes)", Math.Round((double)resLength / (1024 * 1024), 2), resLength);
            }
            else
            {
                return string.Format("{0}KB ({1}bytes)", Math.Round((double)resLength / (1024), 2), resLength);
            }
        }

        public static string GetTimeStr(long downTime)
        {
            if (downTime >= (long)60 * 60 * 1000)
            {
                return string.Format("{0}小时 ({1}毫秒)", Math.Round((double)downTime / (1000 * 60 * 60), 2), downTime);
            }
            else if (downTime >= (long)60 * 1000)
            {
                return string.Format("{0}分钟 ({1}毫秒)", Math.Round((double)downTime / 1000 / 60), downTime);
            }
            else
            {
                return string.Format("{0}毫秒", downTime);
            }
        }

        public static string GetMediaTimeLen(string path)
        {
            try
            {
                //ShellClass shell = new ShellClass();  
                Shell32.Shell shell = new Shell32.Shell();
                //文件路径
                Shell32.Folder folder = shell.NameSpace(path.Substring(0, path.LastIndexOf("\\")));
                //文件名称
                Shell32.FolderItem folderitem = folder.ParseName(path.Substring(path.LastIndexOf("\\") + 1));

                return (Environment.OSVersion.Version.Major >= 6) ? folder.GetDetailsOf(folderitem, 27) : folder.GetDetailsOf(folderitem, 21);
            }
            catch (Exception)
            {
                return "00:00:00";
            }

        }

        /// <summary>
        /// 读取文本小文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string[] ReadSmallText(string filePath)
        {
            string[] rtn = new string[10];
            if (File.Exists(filePath))
            {
                rtn = File.ReadAllLines(filePath);
            }
            return rtn;
        }

        #region base64
        /// <summary>
        /// Base64 编码
        /// </summary>
        /// <param name="encode">编码方式</param>
        /// <param name="source">要编码的字符串</param>
        /// <returns>返回编码后的字符串</returns>
        public static string EncodeBase64(Encoding encode, string source)
        {
            string result = "";
            byte[] bytes = encode.GetBytes(source);
            try
            {
                result = Convert.ToBase64String(bytes);
            }
            catch
            {
                result = source;
            }
            return result;
        }


        /// <summary>
        /// Base64 解码
        /// </summary>
        /// <param name="encode">解码方式</param>
        /// <param name="source">要解码的字符串</param>
        /// <returns>返回解码后的字符串</returns>
        public static string DecodeBase64(Encoding encode, string source)
        {
            string result = "";
            byte[] bytes = Convert.FromBase64String(source);
            try
            {
                result = encode.GetString(bytes);
            }
            catch
            {
                result = source;
            }
            return result;
        }
        #endregion base64
    }
}
